/**
 * 
 */
package u.can.i.up.utils.db.mongo;

import com.mongodb.DBObject;
import com.mongodb.util.JSON;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import u.can.i.up.utils.JsonUtils;

/**
 * @author yanwei
 * @time: 2012-11-12 下午3:04:32
 * @description Mongodb Utils
 * 
 */
public class MongodbUtils {

	/**
	 * convert JSONObject to DBObject
	 * 
	 * @param jsonObj
	 * @return
	 */
	public static DBObject jsonObjToDBObj(JSONObject jsonObj) {
		return (DBObject) JSON.parse(jsonObj.toString());
	}

	/**
	 * convert bean to DBObject
	 * 
	 * @param bean
	 * @return
	 */
	public static DBObject beanToDBObj(Object bean) {
		return (DBObject) JSON.parse(JsonUtils.serialize(bean));
	}

	/**
	 * convert bean to DBObject with jsonConfig
	 * 
	 * @param bean
	 * @param jsonConfig
	 * @return
	 */
	public static DBObject beanToDBObj(Object bean, JsonConfig jsonConfig) {
		return (DBObject) JSON.parse(JsonUtils.serialize(bean, jsonConfig));
	}
}
